<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="query.keyword" placeholder="请输入标签名进行搜索" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      style="margin-top: 20px;"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="100">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center">
        <template slot-scope="{row}">
          {{ row.username }}
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" width="100">
        <template slot-scope="{row}">
          <el-image style="width: 60px" :preview-src-list="[row.avatar_url]" :src="(row.avatar_url | getImage)  || defaultAvatar"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" width="150">
        <template slot-scope="{row}">
          {{ row.phone }}
        </template>
      </el-table-column>
      <el-table-column label="真实姓名" align="center" width="100">
        <template slot-scope="{row}">
          {{ row.name }}
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center">
        <template slot-scope="{row}">
          {{ row.address || '无' }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" width="150">
        <template slot-scope="{row}">
          <span
          >{{ row.createAt | dayFormatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="100">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="small" @click="handleUpdate(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      style="text-align: right"
      v-show="total>0"
      :total="total"
      :page.sync="query.offset"
      :limit.sync="query.limit"
      @pagination="getList"
    />

    <el-dialog :visible.sync="dialogFormVisible" :title="textMap[dialogStatus]">
      <el-form ref="dataForm" :model="temp" label-width="80px" label-position="left">
        <el-form-item label="用户名">
          <span>{{ temp.username }}</span>
        </el-form-item>
        <el-form-item label="联系方式">
          <span>{{ temp.phone }}</span>
        </el-form-item>
        <el-form-item label="真实姓名">
          <span>{{ temp.name }}</span>
        </el-form-item>
        <el-form-item label="地址">
          <span>{{ temp.address || '无' }}</span>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import { userList } from '@/api/user'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves' // waves directive

export default {
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'gray',
        1: 'success',
        2: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      total: 0,
      query: {
        limit: 10,
        offset: 1,
        keyword: '',
        permission: 0
      },
      textMap: {
        update: '详情',
        create: '新增'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      list: null,
      listLoading: true,
      temp: {
        id: '',
        username: '',
        name: '',
        phone: '',
        address: ''
      },
      defaultAvatar: 'https://img0.baidu.com/it/u=705667404,1537851692&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.query.offset = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      userList(this.query).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      if (this.$refs['dataForm']) {
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      }
    }
  }
}
</script>
